package com.lianjing.sh.acceleo.controller.mvc.controller.io.output;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Property;

import com.lianjing.sh.acceleo.controller.tool.service.NameService;

public class OutputGenerate_detail_Service {
	
	private static final Output_Service Output_Service = new Output_Service();
	private static final NameService Name_Service = new NameService();
	
	/**
	 * [
	 * 	[groupNumber, Sequence(Sequence(Property))],
	 * 	[groupNumber, Sequence(Sequence(Property))]
	 * ]
	 * @param aClass
	 */
	public List<Object> getPropertyPathFieldsByGroup(Classifier aClass){
		Map<String, List<List<Property>>> resultMap = new LinkedHashMap<>();
		for(List<Property> ps : Output_Service.getPropertyPathFields(aClass)){
			if(ps.isEmpty()){
				continue;
			}
			//
			List<String> paras = Name_Service.getNameAttrParas(ps.get(ps.size()-1), "g");
			if(null==paras || paras.isEmpty()){
				List<List<Property>> result = resultMap.get("0");
				if(result == null){
					result = new ArrayList<>();
					resultMap.put("0", result);
				}
				result.add(ps);
			}else{
				String groupNumber = paras.get(0);
				List<List<Property>> result = resultMap.get(groupNumber);
				if(result == null){
					result = new ArrayList<>();
					resultMap.put(groupNumber, result);
				}
				result.add(ps);
			}
		}
		//
		List<Object> resultList = new ArrayList<>();
		for(Entry<String, List<List<Property>>> result : resultMap.entrySet()){
			List<Object> list = new ArrayList<>();
				list.add(result.getKey());
				list.add(result.getValue());
			resultList.add(list);
		}
		return resultList;
	}

	/**
	 * [
	 * 	[groupNumber, Sequence(Property)],
	 * 	[groupNumber, Sequence(Property)]
	 * ]
	 */
	public List<Object> getPropertyPathFieldsByGroup(List<Property> ps){
		Map<String, List<Property>> resultMap = new LinkedHashMap<>();
		for(Property p : ps){
			List<String> paras = Name_Service.getNameAttrParas(p, "g");
			if(null==paras || paras.isEmpty()){
				List<Property> result = resultMap.get("0");
				if(result == null){
					result = new ArrayList<>();
					resultMap.put("0", result);
				}
				result.add(p);
			}else{
				String groupNumber = paras.get(0);
				List<Property> result = resultMap.get(groupNumber);
				if(result == null){
					result = new ArrayList<>();
					resultMap.put(groupNumber, result);
				}
				result.add(p);
			}
		}
		//
		List<Object> resultList = new ArrayList<>();
		for(Entry<String, List<Property>> result : resultMap.entrySet()){
			List<Object> list = new ArrayList<>();
				list.add(result.getKey());
				list.add(result.getValue());
			resultList.add(list);
		}
		return resultList;
	}
	
}
